﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCol : MonoBehaviour
{
    public float jiasudu = 1.8f;
    private Rigidbody rigid;
    private float distence;
    private float velocity;

    // Start is called before the first frame update
    private void Start()
    {
        distence = 0;
        rigid = GetComponent<Rigidbody>();
    }

    private Vector3 v3Chu, v3Zhong;
    private Vector3 offect;

    private void FixedUpdate()
    {
        //if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
        //{
        //    Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
        //    float temp = Mathf.Sqrt(Mathf.Pow(touchDeltaPosition.x, 2f) + Mathf.Pow(touchDeltaPosition.y, 2f));
        //    // 路程
        //    distence += temp;
        //    //速度
        //    velocity = Mathf.Abs(temp / Time.deltaTime);
        //    print(velocity);
        //}
        //else
        //{
        //    distence = 0;
        //    print(velocity);
        //}
        if (Input.GetMouseButtonDown(0))
        {
            v3Chu = Input.mousePosition;
        }
        else if (Input.GetMouseButton(0))
        {
            v3Zhong = Input.mousePosition;
            offect = v3Zhong - v3Chu;
            v3Chu = v3Zhong;
            offect = new Vector3(offect.x, 0f, offect.y);
            rigid.AddForce(offect * jiasudu, ForceMode.Acceleration);
        }
        else
        {
            rigid.MovePosition(transform.position + Vector3.forward * 0.02f);
        }
        float z = Camera.main.transform.position.z;
        z = Mathf.Clamp(transform.position.z, Camera.main.transform.position.z + 2.7f, Mathf.Infinity);
        float x = Mathf.Clamp(transform.position.x, -4.2f, 4.2f);
        transform.position = new Vector3(x, transform.position.y, z);
    }
}